import bp
import struct

if __name__ == '__main__':
    faddr = "/sdcard/Git"

    nn = bp.BPNeuralNetwork()
    with open(faddr + "/pybp/mnist/train-labels-idx1-ubyte","rb") as trlf :
      trlb = trlf.read()
    mn,co = struct.unpack("!2I",trlb[0:8])
    print(mn,co)
    labels = []
    for i in range(1,co+1) :
      la = struct.unpack("!B",trlb[7+i:8+i])
      lab = [0,0,0,0,0,0,0,0,0,0]
      lab[la[0]] = 1
      labels.append(lab)
      
    print(labels[0])
    
    with open(faddr + "/pybp/mnist/train-images-idx3-ubyte","rb") as trif :
      trib = trif.read()
    mn,co,row,col = struct.unpack("!4I",trib[0:16])
    print(mn,co,row,col)
    images = []
    for i in range(1,co+1) :
      imb = struct.unpack("!784B",trib[16+(i-1)*784:16+i*784])
      im = []
      for j in range(0,len(imb)) :
        im.append(imb[j]/255) 
      images.append(im)
     
    nn.setup(784, 88, 10)
    nn.train(images, labels, 10000, 0.05, 0.1)
    for case in cases:
      print(nn.predict(case))
